package com.yidu.TAManagement.controller;

import com.yidu.TAManagement.entity.TABusiness;
import com.yidu.TAManagement.service.TABusinessService;
import com.yidu.Util.util.Dbutil;
import com.yidu.Util.util.TableIdUtil;
import com.yidu.Util.util.TableNameUtil;
import com.yidu.Util.util.TableTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class TABusinessController {
    @Resource
    private TABusinessService taBusinessService;
    @Autowired
    private Dbutil dbutil;
    /**
     * 增加基金
     * @param taBusiness 基金对象
     * @return 返回1 则正确 反之 则失败
     */
    @PostMapping("/insertTABusiness")
    public int insertTABusiness(TABusiness taBusiness, HttpServletRequest request){
        HttpSession session = request.getSession();
        String fundId = (String) session.getAttribute("fundId");
        taBusiness.setFundId(fundId);
        String maxId3 = dbutil.getMaxId3(TableIdUtil.TAID, TableNameUtil.TA, TableTimeUtil.TATime, taBusiness.getBusinessTime());
        taBusiness.setTaId(maxId3);
        return taBusinessService.insertTABusiness(taBusiness);
    }
    /**
     * 修改基金
     * @param taBusiness 基金对象
     * @return 返回1 则正确 反之 则失败
     */
    @PutMapping("/updateTABusiness")
    public int updateTABusiness(TABusiness taBusiness){
        return taBusinessService.updateTABusiness(taBusiness);
    }
    /**
     * 删除基金
     * @param taId 基金id
     * @return 返回1 则正确 反之 则失败
     */
    @DeleteMapping("/deleteTABusiness")
    public int deleteTABusiness(String taId){
        System.out.println(taId+"删除");
        return taBusinessService.deleteTABusiness(taId);
    }

    /**
     * 按条件查询加分页
     * @param page   页码
     * @param limit 每页显示的行数
     * @param  taBusiness 查询的条件
     * @return 返回用户的集合
     */
    @GetMapping("/selectTABusiness")
    public Map selectTABusiness(int page,int limit,TABusiness taBusiness){
        System.out.println(page);
        System.out.println("TABusiness="+taBusiness);
        HashMap ta = new HashMap<>();
        List<TABusiness>taBusinessList=taBusinessService.selectTABusiness(page, limit, taBusiness);
        int count =taBusinessService.total(taBusiness);
        ta.put("data",taBusinessList);
        ta.put("code",0);
        ta.put("msg","");
        ta.put("count",count);
return  ta;
    }

}
